﻿using BattleFury.Collections;
using BattleFury.Maps;
using BattleFury.Time;
using Microsoft.Xna.Framework;

namespace BattleFury
{
	public class World
	{
		public bool IsEnabled { get; set; }
		public bool IsVisible { get; set; }
		public MapCollection Chunks { get; set; }
		public WorldTime WorldTime { get; set; }
		public Vector3 Size { get; set; }
		public Vector3 CurrentChunkLocation { get; set; }
		public Vector2 ChunkSize { get; set; }

		public IMap CurrentChunk
		{
			get { return Chunks[CurrentChunkLocation]; }
		}

		public World()
		{
			Chunks = new MapCollection();
			IsEnabled = true;
			IsVisible = true;
			WorldTime = new WorldTime();
			Size = new Vector3(25, 25, 2);
			CurrentChunkLocation = Vector3.Zero;
			ChunkSize = new Vector2(880, 880);
		}

		public virtual void Update(GameTime gameTime)
		{
			if (!IsEnabled) return;

			WorldTime.Update(gameTime);

			for (var i = 0; i < Chunks.Count; i++)
			{
				var chunk = Chunks[i];
				chunk.Update(gameTime);
			}
		}

		public virtual void Draw(GameTime gameTime)
		{
			if (!IsVisible) return;

			for (var i = 0; i < Chunks.Count; i++)
			{
				var chunk = Chunks[i];
				chunk.Draw(gameTime);
			}
		}

		public Vector3 NextAvailableChunk()
		{
			for (var z = 0; z < Size.Z; z++)
			{
				for (var y = 0; y < Size.Y; y++)
				{
					for (var x = 0; x < Size.X; x++)
					{
						var loc = new Vector3(x, y, z);
						if (!Chunks.ContainsKey(loc)) return loc;
					}
				}
			}
			return Vector3.One * -1;
		}
	}
}